package ContainerLayer;
import java.util.ArrayList;

/**
 * Write a description of class SalesmanContainer here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class SalesmanContainer
{
    // instance variables - replace the example below with your own
    private ArrayList<Salesman> salesmen;
    private static SalesmanContainer instance;

    /**
     * Constructor for objects of class SalesmanContainer
     */
    private SalesmanContainer()
    {
        // initialise instance variables
        salesmen = new ArrayList<Salesman>();
        
    }

    public static SalesmanContainer getInstance()
    {
        if(instance == null)
        {
            instance = new SalesmanContainer();
        }
        return instance;       
    }

    public void addSalesman(Salesman sm)
    {

        if(!salesmen.equals(sm))
        {
            salesmen.add(sm);
        }
        else 
        {
            System.out.println("There is already a salesman with the typed name");
        } 

    }

    public Salesman readSalesman(long id)
    {
        int index = 0; 
        int salesmanIndex = 0;
        boolean found = false;

        while(index < salesmen.size() && !found)
        {
            if(salesmen.get(index).getID() == id)
            {                
                found = true; 
                salesmanIndex = index;
            }
            index++;
        }
        if(found)
        {
            return salesmen.get(salesmanIndex);
        }

        else
        {
            return null;
        }
    }

    public void updateSalesman(long id, String name, String address, String mail, int phoneNo)
    {
        int index = 0;
        int salesmanIndex = 0;
        boolean found = false;

        while(index < salesmen.size() && !found)
        {
            if(salesmen.get(index).getID() == id)
            {
                found = true;
                salesmanIndex = index;
            }          
            index++;
        }

        //If found, the filled parametre will be set
        // if not, the unfilled parametre will not be set
        if(found)
        {
            Salesman s = salesmen.get(salesmanIndex);
            if(name != null)
            {
                s.setName(name);
            }
            if(address != null)
            {
                s.setAddress(address);
            }
            if(mail != null)
            {
                s.setMail(mail);
            }
            if(phoneNo != 0)
            {
                s.setPhoneNo(phoneNo);
            }
            
        }

        else
        {
            System.out.println("There is no salesman with the typed ID,  - try again with another ID");
        }
    }

    public void deleteSalesman(long id)
    {   
        int index = 0;
        boolean deleted = false;
        while(index < salesmen.size() && !deleted)
        {
            if(salesmen.get(index).getID() == id)
            {
                salesmen.remove(index);
                deleted = true;
            }
            index++;                       
        }       

        if(!deleted)
        {
            System.out.println("There is no salesman with the typed ID, - try again with another ID");
        }

    }
    
    public void printOutSalesmanID(long id)
    {
        int index = 0; 
        int salesmanIndex = 0;
        boolean found = false;

        while(index < salesmen.size() && !found)
        {
            if(salesmen.get(index).getID() == id)
            {                
                found = true; 
                salesmanIndex = index;
            }
            index++;
        }
        if(found)
        {
            Salesman s = salesmen.get(salesmanIndex);
            System.out.println("############################################################");
                System.out.println("#     Salesman ID: "+s.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("#        name: "+s.getName());
                System.out.println("#     address: "+s.getAddress());
                System.out.println("#    phone no: "+s.getPhoneNo());
                System.out.println("#        mail: "+s.getMail());                
                System.out.println("#");                
                System.out.println("########   THE END  for salesman: "+s.getID());
                System.out.println();  
        }           
    }

    public void printOutAllSalesmen()
    {
        
        if(salesmen.size() != 0)
        {
            System.out.println("    SHOWS INFORMATION ABOUT ALL REGISTERED Salesmen");
            for(Salesman s : salesmen)
            {
                System.out.println("############################################################");
                System.out.println("#     Salesman ID: "+s.getID());
                System.out.println("#  ---------------------------------------------------------");
                System.out.println("#        name: "+s.getName());
                System.out.println("#     address: "+s.getAddress());
                System.out.println("#    phone no: "+s.getPhoneNo());
                System.out.println("#        mail: "+s.getMail());                
                System.out.println("#");                
                System.out.println("########   THE END  for salesman: "+s.getID());
                System.out.println();                
            }
        }
        
        
    }

    public int getSizeSalesman()
    {
        if(salesmen != null)
        {
            return salesmen.size();
        }
        else
        {
            return 0;
        }

    }

}
